/*
 * Copyright (c) 2012 Sqpoll.
 * All rights reserved.
 */
package com.yglab.sqpoll.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.yglab.sqpoll.dao.SurveyMapper;
import com.yglab.sqpoll.model.AnswerChoice;
import com.yglab.sqpoll.model.Question;
import com.yglab.sqpoll.model.QuestionItem;
import com.yglab.sqpoll.model.Survey;
import com.yglab.sqpoll.service.SurveyService;

/**
 * An implemented class for survey service.
 * 
 * @author Younggue Bae
 */
public class SurveyServiceImpl implements SurveyService {

	@Autowired
	private SurveyMapper surveyMapper;
	
	@Override
	public void setSurveyMapper(SurveyMapper surveyMapper) {
		this.surveyMapper = surveyMapper;
	}

	@Override
	public Survey getSurvey(int surveyId) {
		return this.surveyMapper.selectSurvey(surveyId);
	}

	@Override
	public List<Survey> getSurveys(Survey paramters) {
		return this.surveyMapper.selectSurveys(paramters);
	}

	@Override
	public int addSurvey(Survey survey) {
		this.surveyMapper.insertSurvey(survey);
		return survey.getSurveyId();
	}

	@Override
	public void setSurvey(Survey survey) {
		this.surveyMapper.updateSurvey(survey);
	}

	@Override
	public void trashSurvey(int surveyId, String userId) {
		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("surveyId", surveyId);
		parameters.put("userId", userId);
		
		this.surveyMapper.trashSurvey(parameters);
	}

	@Override
	public void deleteSurvey(int surveyId) {
		this.surveyMapper.deleteSurvey(surveyId);
	}
	
	
	@Override
	public Question getQuestion(int surveyId, int questionId) {
		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("surveyId", surveyId);
		parameters.put("questionId", questionId);
		
		Question question = surveyMapper.selectQuestion(parameters);
		List<QuestionItem> questionItems = surveyMapper.selectQuestionItems(parameters);
		List<AnswerChoice> answerChoices = surveyMapper.selectAnswerChoices(parameters);
		
		question.setQuestionItems(questionItems);
		question.setAnswerChoices(answerChoices);
		
		return question;
	}

	@Override
	public List<Question> getQuestions(int surveyId) {
		List<Question> questions = surveyMapper.selectQuestions(surveyId);
		
		for (Question question : questions) {
			Map<String, Object> parameters = new HashMap<String, Object>();
			parameters.put("surveyId", question.getSurveyId());
			parameters.put("questionId", question.getQuestionId());
			
			List<QuestionItem> questionItems = surveyMapper.selectQuestionItems(parameters);
			List<AnswerChoice> answerChoices = surveyMapper.selectAnswerChoices(parameters);
			
			question.setQuestionItems(questionItems);
			question.setAnswerChoices(answerChoices);
		}
		
		return questions;
	}

	@Override
	public int addQuestion(Question question) {
		List<QuestionItem> questionItems = question.getQuestionItems();
		List<AnswerChoice> answerChoices = question.getAnswerChoices();
		
		surveyMapper.insertQuestion(question);
		
		for (QuestionItem questionItem : questionItems) {
			surveyMapper.insertQuestionItem(questionItem);
		}
		
		for (AnswerChoice answerChoice : answerChoices) {
			surveyMapper.insertAnswerChoice(answerChoice);
		}
		
		return question.getQuestionId();
	}

	@Override
	public void setQuestion(Question question) {
		List<QuestionItem> questionItems = question.getQuestionItems();
		List<AnswerChoice> answerChoices = question.getAnswerChoices();
		
		surveyMapper.updateQuestion(question);
		
		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("surveyId", question.getSurveyId());
		parameters.put("questionId", question.getQuestionId());
		
		surveyMapper.deleteQuestionItems(parameters);
		surveyMapper.deleteAnswerChoices(parameters);
		
		for (QuestionItem questionItem : questionItems) {
			surveyMapper.insertQuestionItem(questionItem);
		}
		
		for (AnswerChoice answerChoice : answerChoices) {
			surveyMapper.insertAnswerChoice(answerChoice);
		}
	}

	@Override
	public void deleteQuestion(int surveyId, int questionId) {
		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("surveyId", surveyId);
		parameters.put("questionId", questionId);
		
		surveyMapper.deleteQuestion(parameters);
		surveyMapper.deleteQuestionItems(parameters);
		surveyMapper.deleteAnswerChoices(parameters);
	}

}
